<?php

$dir = $argv[1];
$sqlitefile = $argv[2];
$startdate = $argv[3];
$enddate = $argv[4];

$reportdir = $dir.'/report';
$error = '';
$reporthtml = '';
$gdata = array();
$glabels = array();

include($dir.'/config.php');
include($dir.'/lib.php');

if(!is_dir($reportdir)){
 mkdir($reportdir);
}

if(!file_exists($sqlitefile)){
 print_error('DB file not found');
}

$dbh = new PDO('sqlite:'.$sqlitefile);

$stmt = $dbh->query($sql);

$result = $stmt->setFetchMode(PDO::FETCH_NUM);

$table = new stdclass();
$table->head  = $thead;
$table->size  = $tsize;
$table->align = $talign;
$table->width = $twidth;
  
while($row = $stmt->fetch()){
 $table->data[]  = $row;
 $glabels[] = $row[$rlabel];
 $gdata[] = $row[$rdata];
}

//Graph
pie_graph($gdata,$glabels,$reportdir.'/gpie.png');

radar_graph($gdata,$glabels,$reportdir.'/gradar.png');

$tags = array('{table_categories}','{name}','{error}');
$contents = array(print_table($table, true),$name,'');

$reporthtml = str_replace($tags, $contents, file_get_contents($dir.'/report.tpl.html'));

file_put_contents($reportdir.'/index.html', $reporthtml);

?>